<!--
 * @Author: HeAo
 * @Date: 2021-12-22 16:33:02
 * @LastEditTime: 2022-01-06 16:40:59
 * @LastEditors: Chips
 * @Description: 
 * @FilePath: \am-ui\src\pages\am\monitor\RealTime.vue
 * 别乱动！
-->
<template>
  <div class="realtime">
    <div class="content">
      <div class="left">
        <el-input
          class="search"
          placeholder="请输入设备关键字"
          v-model="filterText"
          style="width: 97%; margin-top: 10px; margin-bottom: 10px"
        >
        </el-input>
        <!-- <img src="..\..\..\icons\svg\搜索.png" /> -->
        <el-tree
          class="filter-tree"
          :data="edTree"
          :props="props"
          @node-click="handleNodeClick"
          :filter-node-method="filterNode"
          ref="tree"
        >
        </el-tree>
        <div class="action">☝　选择设备</div>
      </div>
      <div class="right">
        <div v-show="todayData.length == 0" class="action_1">无数据</div>
        <!-- 标签页 -->
        <div v-show="todayData.length > 0">
          <!-- <el-tabs v-if="activeName" v-model="activeName" @tab-click="handleClick">
            改设备无监测数据
          </el-tabs> -->
          <el-tabs
            v-model="activeName"
            @tab-click="tagHandleClick"
            style="width: 98%"
          >
            <el-tab-pane label="tsp" name="tsp"></el-tab-pane>
            <el-tab-pane label="pm10" name="pm10"></el-tab-pane>
            <el-tab-pane label="pm2.5" name="pm25"></el-tab-pane>
            <el-tab-pane label="温度" name="temperature"></el-tab-pane
            ><el-tab-pane label="湿度" name="humidity"></el-tab-pane>
          </el-tabs>
          <!-- 图表容器 -->
          <div
            ref="realTime"
            id="realTime"
            style="height: 500px; width: 98%"
          ></div>
          <!-- {{ todayData }} -->
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { get } from "@/utils/request.js";
import moment from "moment";
import { area, Line, Area } from "@antv/g2plot";
export default {
  data() {
    return {
      edTree: [],
      props: {
        label: "name",
        value: "id",
      },
      params: {
        page: 1,
        pageSize: 50,
      },
      todayData: [],
      activeName: "tsp",
      chart: null,
      item: [],
      filterText: "",
    };
  },
  watch: {
    //监测输入框内容
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  computed: {
    //计算属性
    // 取出date,和activeName//数据重组
    chartData() {
      return this.todayData.map((item) => {
        return {
          date: moment(item.insert_time).format("hh:mm"),
          [this.activeName]: parseInt(item[this.activeName]), //做活，中括号访问法
        };
      });
    },
  },
  methods: {
    //对树节点进行筛选时执行的方法，返回 true 表示这个节点可以显示，返回 false 则表示这个节点会被隐藏。
    filterNode(value, edTree) {
      // console.log(edTree.name.indexOf(value));
      //value是输入框输入的值
      if (!value) return true;

      //返回名称与输入值相同的edTree
      //indexOf(value)==-1不显示
      return edTree.name.indexOf(value) !== -1;
    },
    // 图表初始化方法
    loadCharts() {
      //删除旧图表
      if (this.chart) {
        this.chart.destroy();
      }
      // 生成图表实例，并配置配置项
      this.chart = new Area("realTime", {
        data: this.chartData,
        padding: "auto",
        xField: "date",
        yField: this.activeName,
        areaStyle: {
          fill: "l(270) 0:#ffffff 1:#6A9955", //渐变色
          cursor: "point",
        },
        line: {
          style: {
            stroke: "#4FBC8D",
          },
        },
      });

      // 开始绘制图表
      this.chart.render();
    },

    //tag点击回调
    tagHandleClick() {
      this.chart.update({
        //更新y轴和数据
        data: this.chartData,
        yField: this.activeName,
      });
    },
    //树形节点点击事件
    handleNodeClick(data) {
      // console.log(data);
      //点击工程不查询
      if (data.engineer_id) {
        this.params = {
          ...this.params,
          engineer_id: data.engineer_id,
          device_id: data.device_id,
        }; // 调用请求数据的方法
        this.queryTodayData();
      }
    },
    //查询工程设备树
    findEngineerDeviceTree() {
      get("/engineer/findEngineerDeviceTree").then((res) => {
        let arr = res.data;
        arr.forEach((element, index) => {
          //遍历arr
          if (element.children.length == 0) {
            //将空的删掉
            delete arr[index];
          }
        });
        // console.log(arr);
        var r = arr.filter((item) => {
          //过滤掉空的
          return item !== null || item !== undefined || item !== "";
        });
        // console.log(r);
        this.edTree = r;
      });
    },
    //根据工程设备ID查询当天设备数据
    queryTodayData() {
      get("/monitor/pageQueryTodayData", this.params).then((res) => {
        this.todayData = res.data.list;
        this.loadCharts();
      });
    },
  },
  created() {
    this.findEngineerDeviceTree();
  },
};
</script>
<style scoped>
.content {
  display: flex;
}
.left {
  width: 30%;
  border-right: 1px solid #d9d9d9;
}
.right {
  margin-left: 20px;
  width: 70%;
}
.action {
  height: 300px;
  line-height: 300px;
  text-align: center;
  font-size: 30px;
  color: #999999;
}
.action_1 {
  height: 500px;
  line-height: 500px;
  text-align: center;
  font-size: 60px;
  font-family: 微软雅黑;
  font-weight: 900;
  color: #e91111;
}
::v-deep .el-icon-caret-right:before {
  content: "\e723";
  font-size: 16px;
}

::v-deep .expanded:before {
  content: "\e722";
  font-size: 16px;
}
</style>